<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="prev" href="optparse-adding-new-types.html" />
<link rel="parent" href="optparse-extending-optparse.html" />
<link rel="next" href="module-getopt.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>14.3.5.2 Adding new actions</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.3.5.1 adding new types"
  href="optparse-adding-new-types.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.3.5 extending optparse"
  href="optparse-extending-optparse.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.4 getopt  "
  href="module-getopt.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="optparse-adding-new-types.html">14.3.5.1 Adding new types</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-extending-optparse.html">14.3.5 Extending optparse</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-getopt.html">14.4 getopt  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h3><a name="SECTION0016352000000000000000"></a><a name="optparse-adding-new-actions"></a>
<br>
14.3.5.2 Adding new actions
</h3>

<p>
Adding new actions is a bit trickier, because you have to understand
that <tt class="module">optparse</tt> has a couple of classifications for actions:
<dl>
<dt><strong>``store'' actions</strong></dt>
<dd>actions that result in <tt class="module">optparse</tt> storing a value to an attribute of the
current OptionValues instance; these options require a <tt class="member">dest</tt>
attribute to be supplied to the Option constructor
</dd>
<dt><strong>``typed'' actions</strong></dt>
<dd>actions that take a value from the command line and expect it to be
of a certain type; or rather, a string that can be converted to a
certain type.  These options require a <tt class="member">type</tt> attribute to the
Option constructor.
</dd>
</dl>

<p>
These are overlapping sets: some default ``store'' actions are <code>store</code>,
<code>store_const</code>, <code>append</code>, and <code>count</code>, while the default ``typed''
actions are <code>store</code>, <code>append</code>, and <code>callback</code>.

<p>
When you add an action, you need to categorize it by listing it in at
least one of the following class attributes of Option (all are lists of
strings):
<dl>
<dt><strong><tt class="member">ACTIONS</tt></strong></dt>
<dd>all actions must be listed in ACTIONS
</dd>
<dt><strong><tt class="member">STORE_ACTIONS</tt></strong></dt>
<dd>``store'' actions are additionally listed here
</dd>
<dt><strong><tt class="member">TYPED_ACTIONS</tt></strong></dt>
<dd>``typed'' actions are additionally listed here
</dd>
<dt><strong><code>ALWAYS_TYPED_ACTIONS</code></strong></dt>
<dd>actions that always take a type (i.e. whose options always take a
value) are additionally listed here.  The only effect of this is
that <tt class="module">optparse</tt> assigns the default type, <code>string</code>, to options with no
explicit type whose action is listed in <code>ALWAYS_TYPED_ACTIONS</code>.
</dd>
</dl>

<p>
In order to actually implement your new action, you must override
Option's <tt class="method">take_action()</tt> method and add a case that recognizes your
action.

<p>
For example, let's add an <code>extend</code> action.  This is similar to the
standard <code>append</code> action, but instead of taking a single value from
the command-line and appending it to an existing list, <code>extend</code> will
take multiple values in a single comma-delimited string, and extend an
existing list with them.  That is, if <code>"-names"</code> is an <code>extend</code>
option of type <code>string</code>, the command line
<div class="verbatim"><pre>
--names=foo,bar --names blah --names ding,dong
</pre></div>

<p>
would result in a list
<div class="verbatim"><pre>
["foo", "bar", "blah", "ding", "dong"]
</pre></div>

<p>
Again we define a subclass of Option:
<div class="verbatim"><pre>
class MyOption (Option):

    ACTIONS = Option.ACTIONS + ("extend",)
    STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
    TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
    ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)

    def take_action(self, action, dest, opt, value, values, parser):
        if action == "extend":
            lvalue = value.split(",")
            values.ensure_value(dest, []).extend(lvalue)
        else:
            Option.take_action(
                self, action, dest, opt, value, values, parser)
</pre></div>

<p>
Features of note:

<ul>
<li> 
<code>extend</code> both expects a value on the command-line and stores that
value somewhere, so it goes in both <tt class="member">STORE_ACTIONS</tt> and
<tt class="member">TYPED_ACTIONS</tt>

<p>
</li>
<li> 
to ensure that <tt class="module">optparse</tt> assigns the default type of <code>string</code> to
<code>extend</code> actions, we put the <code>extend</code> action in
<code>ALWAYS_TYPED_ACTIONS</code> as well

<p>
</li>
<li> 
<tt class="method">MyOption.take_action()</tt> implements just this one new action, and
passes control back to <tt class="method">Option.take_action()</tt> for the standard
<tt class="module">optparse</tt> actions

<p>
</li>
<li> 
<code>values</code> is an instance of the optparse_parser.Values class,
which provides the very useful <tt class="method">ensure_value()</tt> method.
<tt class="method">ensure_value()</tt> is essentially <tt class="function">getattr()</tt> with a safety valve;
it is called as
<div class="verbatim"><pre>
values.ensure_value(attr, value)
</pre></div>

<p>
If the <code>attr</code> attribute of <code>values</code> doesn't exist or is None, then
ensure_value() first sets it to <code>value</code>, and then returns 'value.
This is very handy for actions like <code>extend</code>, <code>append</code>, and
<code>count</code>, all of which accumulate data in a variable and expect that
variable to be of a certain type (a list for the first two, an integer
for the latter).  Using <tt class="method">ensure_value()</tt> means that scripts using
your action don't have to worry about setting a default value for the
option destinations in question; they can just leave the default as
None and <tt class="method">ensure_value()</tt> will take care of getting it right when
it's needed.

<p>
</li>
</ul>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.3.5.1 adding new types"
  href="optparse-adding-new-types.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.3.5 extending optparse"
  href="optparse-extending-optparse.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.4 getopt  "
  href="module-getopt.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="optparse-adding-new-types.html">14.3.5.1 Adding new types</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-extending-optparse.html">14.3.5 Extending optparse</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-getopt.html">14.4 getopt  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
